package cn.hx.whh.automaticticket.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.sunmi.extprinterservice.ExtPrinterService;

import java.util.ArrayList;

import cn.hx.whh.automaticticket.R;
import cn.hx.whh.automaticticket.base.BaseBean;
import cn.hx.whh.automaticticket.constant.Constants;
import cn.hx.whh.automaticticket.constant.HttpUrls;
import cn.hx.whh.automaticticket.dialog.DialogView;
import cn.hx.whh.automaticticket.dialog.TipsDialog;
import cn.hx.whh.automaticticket.dialog.TipsDialogView;
import cn.hx.whh.automaticticket.enums.PrintEnum;
import cn.hx.whh.automaticticket.enums.ScannerHeadEnum;
import cn.hx.whh.automaticticket.listener.OnScannerListener;
import cn.hx.whh.automaticticket.netword.HttpUtils;
import cn.hx.whh.automaticticket.netword.Interfaceabstract.HttpInterface;
import cn.hx.whh.automaticticket.netword.Interfaceabstract.OKHttpListener;
import cn.hx.whh.automaticticket.operation.K2Print;
import cn.hx.whh.automaticticket.params.BarCodeContent;
import cn.hx.whh.automaticticket.params.CheckTicketReport;
import cn.hx.whh.automaticticket.params.CheckTicketResponse;
import cn.hx.whh.automaticticket.params.PrintParams;
import cn.hx.whh.automaticticket.params.QrCodeContent;
import cn.hx.whh.automaticticket.params.TextContent;
import cn.hx.whh.automaticticket.scanner.ScannerHeadProgram;

public class AutomaticTicketActivity extends Activity implements HttpInterface {
    private static final String TAG = "AutomaticTicketActivity";
    private K2Print k2Print = null;
    private ServiceConnection connService = null;
    private ExtPrinterService extPrinterService = null; //k2 打印服务
    private AutomaticTicketActivity mActivity;
    private ScannerHeadProgram scannerHead = null; //k2 扫码读头信息

    private DialogView dialog ;

    OnScannerListener listener = new OnScannerListener(){
        @Override
        public void onScanData(String data, ScannerHeadEnum headEnum){
            System.out.println("AutomaticTicketActivity扫码信息：" +data);
            synchronized(this){
                dialog.show();
                //print(data);
                dsad();
            }
        }
        @Override
        public void onResponseData(String data, ScannerHeadEnum headEnum){
            System.out.println("AutomaticTicketActivity响应信息：" +data);
        }
        @Override
        public void onResponseTimeout(){}
    };
    private void  ticketInspectionPrinting(String data){
        CheckTicketReport report = new CheckTicketReport(null,2,data,data);
        System.out.println(report.toJSONString());
        System.out.println(HttpUrls.show_app_check_tickets);
        final CheckTicketResponse response = new CheckTicketResponse();
         // initDialog();
        HttpUtils.Post(this, HttpUrls.show_app_check_tickets, report.toMapUtils(),null, response, BaseBean.class, new OKHttpListener<BaseBean>() {
            @Override
            public void onSuccess(BaseBean bean) {
                System.out.println("response -->" + response.toString());
                if (bean.code == 200) {
                    TipsDialog tipsDialogView =  new TipsDialog(mActivity,"打印中请勿离开",true);
                    tipsDialogView.show();
                    tipsDialogView.dialog.setCanceledOnTouchOutside(false);
                    // begin  验票成功
                    // response System.out.println(bean.params);
                    printTicket(response);
                    tipsDialogView.hide();

                    // end  验票成功
                }else if (bean.code == 201) {
                    // begin  门票已经使用
                    TipsDialog tipsDialogView = new TipsDialog(mActivity,"门票已经使用",true);
                    tipsDialogView.show();
                    // end  门票已经使用
                }else{

                    TipsDialog tipsDialogView =  new TipsDialog(mActivity,"无效门票",true);
                    tipsDialogView.show();
                    // tipsDialogView.dialog.setCanceledOnTouchOutside(false);
                }
            }

            @Override
            public void handleResources(Object o) {
                /*if (tipsDialogView!=null){
                    tipsDialogView.hide();
                }*/
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        setContentView(R.layout.activity_automatic_ticket);
        ScannerHeadProgram scanner = ScannerHeadProgram.getInstance(AutomaticTicketActivity.this);
        scanner.initScannerHead(ScannerHeadEnum.radio_broadcast);
        scanner.analysisBroadcast();
        scanner.setScannerListener(listener);
        dialog = new DialogView(AutomaticTicketActivity.this);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        // SystemUiUtils.setStickFullScreen(getWindow().getDecorView());  // 全屏化 Activity
    }

    /**
     * 打印票据
     * @param response
     */
    private void printTicket(CheckTicketResponse response){

        Intent intent = new Intent();
        intent.setPackage("com.sunmi.extprinterservice");
        intent.setAction("com.sunmi.extprinterservice.PrinterService");

        ArrayList<PrintParams> params = new ArrayList<>();

        PrintParams stadium = new PrintParams();
        stadium.setType(PrintEnum.Text);
        TextContent stadiumContent = new TextContent();
        stadiumContent.setText("演播厅室: "+response.strStadiumName);
        stadium.setContent(stadiumContent);
        params.add(stadium);

        PrintParams area = new PrintParams();
        area.setType(PrintEnum.Text);
        TextContent areaContent = new TextContent();
        areaContent.setText("座位区域: "+response.strAreaName);
        area.setContent(areaContent);
        params.add(area);

        PrintParams seat = new PrintParams();
        seat.setType(PrintEnum.Text);
        TextContent seatContent = new TextContent();
        seatContent.setText("座位排号: "+response.strSeatInfo);
        seat.setContent(seatContent);
        params.add(seat);

        PrintParams price = new PrintParams();
        price.setType(PrintEnum.Text);
        TextContent priceContent = new TextContent();
        priceContent.setText("票面价格: "+response.ticktPrice);
        price.setContent(priceContent);
        params.add(price);

        /*PrintParams qrCode = new PrintParams();
        qrCode.setType(PrintEnum.QrCode);
        QrCodeContent qrCodeContent=new QrCodeContent();
        qrCodeContent.setCode("zheShiYiGeErWeiMa");
        qrCode.setContent(qrCodeContent);
        params.add(qrCode);*/

        connService = new ServiceConnection() {

            @Override
            public void onServiceDisconnected(ComponentName name) {
                extPrinterService = null;
            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                extPrinterService = ExtPrinterService.Stub.asInterface(service);
                k2Print = new K2Print(AutomaticTicketActivity.this, extPrinterService, params);
                k2Print.title =response.strScheduleName; // 标题 演出名称 节目名称
                k2Print.callback(k2Print,connService);
                k2Print.print();
            }
        };
        //
        bindService(intent, connService, Context.BIND_AUTO_CREATE);

    }

    public void print(View view) {
        Intent intent = new Intent();
        intent.setPackage("com.sunmi.extprinterservice");
        intent.setAction("com.sunmi.extprinterservice.PrinterService");

        ArrayList<PrintParams> params = new ArrayList<>();

        PrintParams text = new PrintParams();
        text.setType(PrintEnum.Text);
        TextContent textContent = new TextContent();
        textContent.setText("任意字符串1");
        text.setContent(textContent);
        params.add(text);

        PrintParams text2 = new PrintParams();
        text2.setType(PrintEnum.Text);
        TextContent textContent2 = new TextContent();
        textContent2.setText("任意字符串2");
        text2.setContent(textContent2);
        params.add(text2);

        PrintParams barCode = new PrintParams();
        barCode.setType(PrintEnum.BarCode);
        BarCodeContent barCodeContent = new BarCodeContent();
        barCodeContent.setCode("12345621231");
        barCode.setContent(barCodeContent);
        params.add(barCode);

        PrintParams qrCode = new PrintParams();
        qrCode.setType(PrintEnum.QrCode);
        QrCodeContent qrCodeContent=new QrCodeContent();
        qrCodeContent.setCode("zheShiYiGeErWeiMa");
        qrCode.setContent(qrCodeContent);
        params.add(qrCode);

        connService = new ServiceConnection() {

            @Override
            public void onServiceDisconnected(ComponentName name) {
                extPrinterService = null;
            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                extPrinterService = ExtPrinterService.Stub.asInterface(service);
                k2Print = new K2Print(AutomaticTicketActivity.this, extPrinterService, params);
                k2Print.title ="《天鹅湖》";
                k2Print.callback(k2Print,connService);
                k2Print.print();
            }
        };
        //
        bindService(intent, connService, Context.BIND_AUTO_CREATE);
    }

    public void print(String data) {
        Intent intent = new Intent();
        intent.setPackage("com.sunmi.extprinterservice");
        intent.setAction("com.sunmi.extprinterservice.PrinterService");

        ArrayList<PrintParams> params = new ArrayList<>();

        PrintParams text = new PrintParams();
        text.setType(PrintEnum.Text);
        TextContent textContent = new TextContent();
        textContent.setText("任意字符串1");
        text.setContent(textContent);
        params.add(text);

        PrintParams text2 = new PrintParams();
        text2.setType(PrintEnum.Text);
        TextContent textContent2 = new TextContent();
        textContent2.setText("任意字符串2");
        text2.setContent(textContent2);
        params.add(text2);

        PrintParams barCode = new PrintParams();
        barCode.setType(PrintEnum.BarCode);
        BarCodeContent barCodeContent = new BarCodeContent();
        barCodeContent.setCode("12345621231");
        barCode.setContent(barCodeContent);
        params.add(barCode);

        PrintParams qrCode = new PrintParams();
        qrCode.setType(PrintEnum.QrCode);
        QrCodeContent qrCodeContent=new QrCodeContent();
        qrCodeContent.setCode("zheShiYiGeErWeiMa");
        qrCode.setContent(qrCodeContent);
        params.add(qrCode);

        connService = new ServiceConnection() {

            @Override
            public void onServiceDisconnected(ComponentName name) {
                extPrinterService = null;
            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                extPrinterService = ExtPrinterService.Stub.asInterface(service);
                k2Print = new K2Print(AutomaticTicketActivity.this, extPrinterService, params);
                k2Print.title ="《天鹅湖》";
                k2Print.callback(k2Print,connService);
                k2Print.print();
            }
        };
        //
        bindService(intent, connService, Context.BIND_AUTO_CREATE);
        dialog.hide();
        startActivity(new Intent(this, ReceiveSuccessActivity.class));
    }

    private void dsad(){
        startActivity(new Intent(this, ReceiveSuccessActivity.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (scannerHead != null)
            scannerHead.destory();
    }

    @Override
    public Activity getActivity() {
        return null;
    }

    @Override
    public boolean isDiscardHttp() {
        return false;
    }
}